/**********************************************************
 * File description:devicemanagement.cpp
 * Function:设备管理类
 * Current Version: 1.0.0
 * Author: 王伟豪
 * Date: 2022-10-02
 *  -----
 *  Copyright by BoKun Software.
 **********************************************************/
#include<DeviceManagementService.h>


DeviceManagement::DeviceManagement()
{
    path = QCoreApplication::applicationDirPath();
    sqlD = new SqliteDriver();
    sqlD->setDBDir(path);
    sqlD->openDB(DBNAME);
};

DeviceManagement::~DeviceManagement()
{
    sqlD->closeDB();
};


/*!
 * \brief 向DEVICE_TABLE中插入数据
 *
 * \param QString name, QString mac,
 * \param QString ip, QString port,
 * \param QString serialNumber, QString status
 *
 * Detail here!!!
 */
void DeviceManagement::create(QString name, QString mac, QString ip, QString port, QString serialNumber, QString status)
{
    QHash<QString,QString> data;

    data["name"] = name;
    data["mac"] = mac;
    data["ip"] = ip;
    data["port"] = port;
    data["serial_number"] = serialNumber;
    data["status"] = status;
    sqlD->addData(DEVICE_TABLE,data);
}

/*!
 * \brief 删除DEVICE_TABLE中某一条数据
 *
 * \param QString id 要删除数据的id号
 *
 * Detail here!!!
 */
void DeviceManagement::remove(QString id)
{
    sqlD->delData(DEVICE_TABLE,"where id = "+id); //删除操作
}



/*!
 * \brief 修改DEVICE_TABLE中某一条数据
 *
 * \param QString id,  要修改的数据的id
 * \param QString name, QString mac,
 * \param QString ip, QString port,
 * \param QString serialNumber, QString status
 *
 * Detail here!!!
 */
void DeviceManagement::revise(QString id, QString name, QString mac, QString ip, QString port, QString serialNumber, QString status)
{
    QHash<QString,QString> data;
    data["name"] = name;
    data["mac"] = mac;
    data["ip"] = ip;
    data["port"] = port;
    data["serial_number"] = serialNumber;
    data["status"] = status;
    data["update_time"] = "DATETIME('now','localtime')";
    sqlD->updateData(DEVICE_TABLE,data,"where id = "+id);
}



/*!
 * \brief 用于获取DEVICE_TABLE中的全部数据
 *
 * \return QList<QHash<QString,QString>> 由qlist封装每一条数据（qhash）
 *
 * Detail here!!!
 */
QList<QHash<QString,QString>> DeviceManagement::show()
{
    QList<QHash<QString,QString>> data;
    sqlD->queryExec("SELECT * FROM device",data);
    return data;
}



/*!
 * \brief 用于刷新ui展示中的某一条数据
 * \param QString id 要刷新数据的id
 * \return QList<QHash<QString,QString>> 由qlist封装每一条数据（qhash）
 *
 * Detail here!!!
 */
QList<QHash<QString,QString>> DeviceManagement::refresh(QString id)
{
    QList<QHash<QString,QString>> data;
    sqlD->queryExec("SELECT * FROM device where id ORDER BY id = " + id,data);
    return data;

}

